/**
 * Created by gongxun on 16/11/18.
 */

var KwxLoginLayer = KwxBaseLayer.extend({
    TextField_uid : null,
    CheckBox_xieyi : null,
    Button_wxLogin : null,

    //移动背景
    mHallBackgroupMove : null,
    ctor : function () {
        this._super();
        this.loadCocostudio("res/ccs/KwxLoginLayer.json");
        //字体修复
        mo.gameHelper.fixIosText(this.ccsNode);

        this.CheckBox_xieyi = mo.seekWidgetByName(this.ccsNode, "CheckBox_xieyi");

        this.TextField_uid = mo.seekWidgetByName(this.ccsNode, "TextField_uid");

        this.Button_wxLogin = mo.seekWidgetByName(this.ccsNode, "Button_wxLogin");
        this.Button_wxLogin.addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {

                //TODO
                // mo.httpClient.getTimestampBaidu(function (isSuccess, timestamp) {
                //
                // });

                // mo.httpHelper.sendGet("http://www.bilibili.tv", function(a, b){
                //     cc.log("a:" + a);
                // });
                //
                // return;

                // mo.testDemo.showDemo();
                //
                // cc.loader.loadJs(["src/testDemo1.js"], function(err){
                //     cc.log("新加载" + err);
                // });
                // for (var key in cc.loader._jsCache){
                //     if (cc.loader._jsCache.hasOwnProperty(key)){
                //         cc.log("key:" + key + " value:" + cc.loader._jsCache.key);
                //     }
                // }
                // return;

                //记录remark
                // mo.loginHelper.saveTryRemark("59.110.22.154:10086");
                // return;

                //出牌规则
                // mo.pdkCardRule.test();
                // return;


                // var xinhaos = [
                //     " vivo vivo X5Max",
                //     "vivo vivo X5Max ",
                //     " vivo vivo X5Max ",
                //     "  vivo vivo X5Max",
                //     "vivo vivo X5Max  ",
                //     "  vivo vivo X5Max  ",
                //     "   vivo vivo X5Max",
                //     "vivo vivo X5Max   ",
                //     "   vivo vivo X5Max   ",
                //     "    vivo vivo X5Max",
                //     "vivo vivo X5Max    ",
                //     "    vivo vivo X5Max    ",
                //     "vivo X5Max"
                // ];
                //
                // for (var i = 0; i < xinhaos.length; i++){
                //     var keys = ["code", "channel", "t", "device"];
                //     var values = ["061CfMQY12910Z0tSWTY1bu0RY1CfMQz", "1", "1502251917", xinhaos[i]];
                //     var postData = mo.nativeHelper.getParamsData(keys, values);
                //
                //     // var keys = ["login_name", "access_token", "t", "device"];
                //     // var values = [loginName, accessToken, timestamp, phoneXinhao];
                //     // var postData = mo.nativeHelper.getParamsData(keys, values);
                //
                //     cc.log("postData:" + postData);
                // }
                //
                //
                // return;

                // cc.spriteFrameCache.addSpriteFrames("res/effect/zha-zhadan.plist", "res/effect/zha-zhadan.png");
                // ccs.armatureDataManager.addArmatureFileInfo("res/effect/zha-zhadan.xml");
                //
                // var zd = new ccs.Armature("zd");
                // zd.setScale(1);
                // zd.setPosition(mo.curSize.x / 2, mo.curSize.y / 2);
                // // zd.animation.setSpeedScale(1);
                // this.addChild(zd);
                //
                // zd.animation.play("ZD_2", 2, 0);

                // cc.spriteFrameCache.addSpriteFrames("res/effect/pdk/woyingle.plist", "res/effect/pdk/woyingle.png");
                // ccs.armatureDataManager.addArmatureFileInfo("res/effect/pdk/woyingle.xml");
                //
                // var anim = new ccs.Armature("woyingle");
                // anim.setScale(0.4);
                // anim.animation.setSpeedScale(1);
                // anim.setPosition(mo.curSize.x / 2, mo.curSize.y / 2);
                // this.addChild(anim);
                // var that = this;
                // anim.animation.setMovementEventCallFunc(function (armature, movementType, movementID) {
                //     cc.log("movementType:" + movementType);
                //     if (movementType == 1){
                //         armature.scheduleOnce(function(dt){
                //             that.removeChild(armature);
                //         }, 0.5);
                //     }
                // });
                //
                // anim.animation.play("woyingle-zong", 2, 0);
                //
                // return;

                if (!this.checkXieyi()){
                    return;
                }

                var userId = this.TextField_uid.getString();
                var num = parseInt(userId);

                // 变灰
                this.Button_wxLogin.setEnabled(false);
                //意外出现超时30秒恢复
                this.Button_wxLogin.runAction(cc.sequence(
                    cc.delayTime(30),
                    cc.callFunc(function(){
                        this.setEnabled(true);
                    }, this.Button_wxLogin)
                ));


                // 登录
                if (mo.gameHelper.isRead){
                    mo.loginHelper.tryLogin(num);
                }else{
                    mo.gameWebsocket.openWebSocket(userId);
                }
            }
        }, this);

        //快速登录
        mo.seekWidgetByName(this.ccsNode, "Button_fast").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                var userId = this.TextField_uid.getString();
                var num = parseInt(userId);
                if (num >= 500 && num <= 3000){
                    mo.gameWebsocket.openWebSocket(num.toString());
                }else{
                    var uid = parseInt(Math.random() * 500 + 1500);
                    mo.gameWebsocket.openWebSocket(uid.toString());
                }
            }
        }, this);

        //用户协议
        mo.seekWidgetByName(this.ccsNode, "Image_userAgreement").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.addChild(new KwxUserAgreementLayer());
            }
        }, this);

        //飘字动画
        this.moveTextAction();

        //test
        var Panel_content_test = mo.seekWidgetByName(this.ccsNode, "Panel_content_test");
        mo.gameHelper.setClippingLayout(Panel_content_test);
    },

    onEnter : function(){
        this._super();

        //读取uid
        var uid = mo.fileHelper.getLoginInfo();
        if (uid){
            this.TextField_uid.setString(uid);
        }else{
            uid = parseInt(Math.random() * 500 + 1500);
            this.TextField_uid.setString(uid.toString());
        }

        //读取cookie
        // var cookie_uid = mo.gameHelper.getCookie("uid");
        // if (cookie_uid != null){
        //     this.TextField_uid.setString(cookie_uid.toString());
        // }else{
        //     this.TextField_uid.setString("为获取cookie?");
        // }

        //设置版本号
        mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_version"), "版本号:v" + mo.gameHelper.version);

        //检测新版本
        this.scheduleOnce(function(dt){
            //版本检测
            mo.gameHelper.checkBaseVersion();

            //维护信息
            // mo.gameHelper.checkWeiHuInfo();
        }, 0.5);

        //检测是否是审核状态
        // var that = this;
        // mo.httpClient.sendCurrentShenheVersion(function(isSuccess, version){
        //     if (isSuccess == true){
        //         mo.gameHelper.updateIsIosCheck(version == mo.gameHelper.shenheVersion);
        //     }
        // });

        //更新ui
        mo.eventHelper.add("updateIsIosCheck", this, function(){
            this.updateUIForIosCheck();
        });

        //登录按钮状态
        mo.eventHelper.add("login_loginBtnCancel", this, function(){
            this.Button_wxLogin.stopAllActions();
            this.Button_wxLogin.setEnabled(true);
        });

        //ios检测
        this.updateUIForIosCheck();

        //重力
        // this.mHallBackgroupMove = new HallBackgroupMove();
        // this.mHallBackgroupMove.startMove(this, mo.seekWidgetByName(this.ccsNode, "Image_bgMove"));
    },

    onExit : function(){
        mo.eventHelper.remove(this);

        // this.mHallBackgroupMove.stopMove();

        this._super();
    },

    moveTextAction : function(){
        var Text_tishi = mo.seekWidgetByName(this.ccsNode, "Text_tishi");
        var time = 5;
        var p1 = cc.p(mo.curSize.x - Text_tishi.getContentSize().width, 24);
        var p2 = cc.p(0, 24);

        Text_tishi.runAction(cc.repeatForever(
            cc.sequence(
                cc.moveTo(time, p1),
                cc.moveTo(time, p2)
            )
        ));
    },

    checkXieyi : function(){
        if (this.CheckBox_xieyi.isSelected()){
            return true;
        }else{
            mo.showMsgDialogOk("您必须先同意用户协议！");
            return false;
        }
    },

    updateUIForIosCheck : function(){
        if (mo.gameHelper.isIosCheck){
            mo.seekWidgetByName(this.ccsNode, "TextField_uid").setVisible(false);
            mo.seekWidgetByName(this.ccsNode, "Image_inputBg").setVisible(false);

            mo.seekWidgetByName(this.ccsNode, "Button_wxLogin").setVisible(false);
            mo.seekWidgetByName(this.ccsNode, "Button_fast").setVisible(true);
        }else{
            mo.seekWidgetByName(this.ccsNode, "TextField_uid").setVisible(!mo.gameHelper.isRead);
            mo.seekWidgetByName(this.ccsNode, "Image_inputBg").setVisible(!mo.gameHelper.isRead);

            mo.seekWidgetByName(this.ccsNode, "Button_wxLogin").setVisible(true);
            mo.seekWidgetByName(this.ccsNode, "Button_fast").setVisible(false);
        }
    }
});